<%!
/**
 *
 */
public static String formatAmount(double dblVal) {
  String Amount = "0.00";
    try {
        NumberFormat nf = NumberFormat.getNumberInstance (); 
        nf.setMinimumFractionDigits(2);
        nf.setMaximumFractionDigits(2);
        Amount = nf.format(dblVal);
    }catch (Exception e){
    }
  return  Amount;
}

/**
 *
 */
public static String toTime(int seconds) {
    String time = "00:00:00";
    try {
        int intHour = seconds/3600;
        int intTemp = seconds%3600;
        int intMin = intTemp/60;
        int intSec = intTemp%60;

        String hour = "";
        if (intHour>9)
            hour = Integer.toString(intHour);
        else
            hour = "0"+Integer.toString(intHour);
        String min = "";
        if (intMin>9)
            min = Integer.toString(intMin);
        else
            min = "0"+Integer.toString(intMin);
        String sec = "";
        if (intSec>9)
            sec = Integer.toString(intSec);
        else
            sec = "0"+Integer.toString(intSec);

        time =  hour + ":" + min + ":" + sec;

    }catch (Exception e){
    }

    return time;
}

/**
 *
 */
public static int getSeconds(String time) {
    int seconds = 0;
    try {
        if (!time.equals("")){
            String tempString = "";
            String hour = "";
            String min = "";
            String sec = "";
            int intHour = 0;
            int intMin = 0;
            int intSec = 0;
            int idx = 0;

            tempString = time;
            idx = tempString.indexOf(':');
            hour = tempString.substring(0,idx);
            tempString = tempString.substring(idx+1);
            idx = tempString.indexOf(':');
            min = tempString.substring(0,idx);
            sec = tempString.substring(idx+1);

            intHour = Integer.parseInt(hour)*60*60;
            intMin = Integer.parseInt(min)*60;
            intSec = Integer.parseInt(sec);

            seconds = intHour + intMin + intSec;
        }
    }catch (Exception e){
    }
    return seconds;
}

/**
 *
 */
public class CallDetailsComparator implements Comparator{

  private boolean isAscending = true;
  private int fieldIndex;
  private ArrayList fieldPropertiesList;

  public CallDetailsComparator(boolean isAscending, int fieldIndex, ArrayList fieldPropertiesList) {
      this.isAscending = isAscending;
      this.fieldIndex = fieldIndex;
      this.fieldPropertiesList = fieldPropertiesList;
  }

  public int compare(Object object1, Object object2) {

    try{
        // Cast Objects
        ArrayList list1 = (ArrayList)object1;
        ArrayList list2 = (ArrayList)object2;

        FieldProperties fp = (FieldProperties) fieldPropertiesList.get(fieldIndex);

        String temp1 = (String) list1.get(fieldIndex);
        String temp2 = (String) list2.get(fieldIndex);

        if (fp.type.equals("String")){
            String tmp1 = temp1;
            String tmp2 = temp2;
            if (this.isAscending) {
                return tmp1.compareTo(tmp2);
            } else {
                return -(tmp1.compareTo(tmp2));
            }
        }
        if (fp.type.equals("Double")){
            if (temp1.equals("")){
                temp1 = "0.00";
            }
            if (temp2.equals("")){
                temp2 = "0.00";
            }
            Double tmp1 = new Double(Double.parseDouble(temp1));
            Double tmp2 = new Double(Double.parseDouble(temp2));
            if (this.isAscending) {
                return tmp1.compareTo(tmp2);
            } else {
                return -(tmp1.compareTo(tmp2));
            }
        }
        if (fp.type.equals("Date")){
            DateFormat df = new SimpleDateFormat("dd/MM/yy");
            if (temp1.indexOf("-")>=0){
                temp1 = temp1.substring(0,temp1.indexOf("-")-1);
            }
            if (temp2.indexOf("-")>=0){
                temp2 = temp2.substring(0,temp2.indexOf("-")-1);
            }
            Date tmp1 = df.parse(temp1);
            Date tmp2 = df.parse(temp2);
            if (this.isAscending) {
                return tmp1.compareTo(tmp2);
            } else {
                return -(tmp1.compareTo(tmp2));
            }
        }
        if (fp.type.equals("Time")){
            if (temp1.equals("")){
                temp1 = "00:00:00";
            }
            if (temp2.equals("")){
                temp2 = "00:00:00";
            }
            Integer tmp1 = new Integer(getSeconds(temp1));
            Integer tmp2 = new Integer(getSeconds(temp2));
            if (this.isAscending) {
                return tmp1.compareTo(tmp2);
            } else {
                return -(tmp1.compareTo(tmp2));
            }
        }
    } catch (Exception e){
        return 0;
    }
    return 0;
  }
}

/**
 *
 */
public class SectionProperties {
    public String key = "";
    public String name = "";
    public String type = "";
    public String title_ENG = "";
    public String title_BM = "";
    public String subTitle_ENG = "";
    public String subTitle_BM = "";

    public ArrayList childSectionKeyList = new ArrayList();
    public ArrayList parentSectionKeyList = new ArrayList();
    public ArrayList fieldKeyList = new ArrayList();

    public StatementSection section = null;
}

/**
 *
 */
public class FieldProperties {
    public String key = "";
    public String name = "";
    public String type = "";
    public int width = 0;
    public String title_ENG = "";
    public String title_BM = "";
    public String title_align = "";
    public String data_align = "";
    public int colspan = 0;
    public String phoneBookTag = "N";
}

/**
 *
 */
public FieldProperties getFieldProperties(String fieldKey) {
    FieldProperties fp = new FieldProperties();

    try {
        Properties appCfg = PropertiesConfig.getApplicationSettings();
        if (appCfg.hasProperty(fieldKey + "_Field")){
            String properties = appCfg.getProperty(fieldKey + "_Field");
            StringTokenizer st = new StringTokenizer((String)properties,"|");
            fp.key = fieldKey;
            if (st.hasMoreTokens()) {
                fp.name = st.nextToken();
            }
            if (st.hasMoreTokens()) {
                fp.type = st.nextToken();
            }
            if (st.hasMoreTokens()) {
                fp.width = Integer.parseInt(st.nextToken());
            }
            if (st.hasMoreTokens()) {
                fp.title_ENG = st.nextToken();
            }
            if (st.hasMoreTokens()) {
                fp.title_BM = st.nextToken();
            }
            if (st.hasMoreTokens()) {
                fp.title_align = st.nextToken();
            }
            if (st.hasMoreTokens()) {
                fp.data_align = st.nextToken();
            }
            if (st.hasMoreTokens()) {
                fp.colspan = Integer.parseInt(st.nextToken());
            }
            if (st.hasMoreTokens()) {
                fp.phoneBookTag = st.nextToken();
            }
        }
    } catch (Exception e){
        // ignore
    }

    return fp;
}

/**
 *
 */
public ArrayList getSectionFields(ArrayList fieldKeyList){
    ArrayList fpList = new ArrayList();
    try {
        Iterator iterator = fieldKeyList.iterator();
        while (iterator.hasNext()){
            fpList.add(getFieldProperties((String)iterator.next()));
        }
    } catch (Exception e){
        // ignore
    }

    return fpList;
}

/**
 *
 */
public String getSectionKey(String searchKey) {
    String key = "";

    try {
        Properties appCfg = PropertiesConfig.getApplicationSettings();

        if (appCfg.hasProperty(searchKey)){
            key = appCfg.getProperty(searchKey);
        }
    } catch (Exception e){
        // ignore
    }

    return key;
}

/**
 *
 */
public SectionProperties getSectionProperties(String key) {
    SectionProperties sp = new SectionProperties();

    try {
        Properties appCfg = PropertiesConfig.getApplicationSettings();
        sp.key = key;
        if (appCfg.hasProperty(key + "_SectionName")){
            sp.name = appCfg.getProperty(key + "_SectionName");
        }
        if (appCfg.hasProperty(key + "_SectionType")){
            sp.type = appCfg.getProperty(key + "_SectionType");
        }
        if (appCfg.hasProperty(key + "_SectionTitle_ENG")){
            sp.title_ENG = appCfg.getProperty(key + "_SectionTitle_ENG");
        }
        if (appCfg.hasProperty(key + "_SectionTitle_BM")){
            sp.title_BM = appCfg.getProperty(key + "_SectionTitle_BM");
        }
        if (appCfg.hasProperty(key + "_SectionSubTitle_ENG")){
            sp.subTitle_ENG = appCfg.getProperty(key + "_SectionSubTitle_ENG");
        }
        if (appCfg.hasProperty(key + "_SectionSubTitle_BM")){
            sp.subTitle_BM = appCfg.getProperty(key + "_SectionSubTitle_BM");
        }
        if (appCfg.hasProperty(key + "_ChildSections")){
            String childSections = appCfg.getProperty(key + "_ChildSections");
            StringTokenizer st = new StringTokenizer((String)childSections,"|");
            while(st.hasMoreTokens()) {
                sp.childSectionKeyList.add(st.nextToken());
            }
        }
        if (appCfg.hasProperty(key + "_Fields")){
            String fields = appCfg.getProperty(key + "_Fields");
            StringTokenizer st = new StringTokenizer((String)fields,"|");
            while(st.hasMoreTokens()) {
                sp.fieldKeyList.add(st.nextToken());
            }
        }
        String parentSections = sp.key;
        StringTokenizer st = new StringTokenizer((String)parentSections,"_");
        String strTemp = "";
        while(st.hasMoreTokens()) {
            strTemp = strTemp + st.nextToken();
            sp.parentSectionKeyList.add(strTemp);
            strTemp = strTemp + "_";
        }
    } catch (Exception e){
        // ignore
    }

    return sp;
}
public double getSectionTotal(StatementSection section){
	double total = 0.00;

	try{
		if (section != null){
			StatementSection totalSection = section.getSection("TotalSection");

			if (totalSection != null){
				try{
					total += getFieldAmount(totalSection, "Total");
				} catch(Exception ex){
				}
			} else{
				int count = (int) section.getSectionCount("RepeatSection");
				for (int index = 0; index < count; index++) {
					StatementSection repeatSection = section.getRepeatSection("RepeatSection", index);

					if (repeatSection != null){
						try{
							total += getFieldAmount(repeatSection, "Total");
						} catch(Exception ex){
						}
					}
				}
			}
		}
	} catch(Exception e){
	}
	return total;
}

public double getFieldAmount(StatementSection section, String field){
	double fieldAmount = 0.00;
	try{
		String text = section.getFieldText(field);
		text = text.toUpperCase();
		text = text.replaceAll(" ","");
		text = text.replaceAll("[,]","");
		text = text.replaceAll("RM","");

		fieldAmount = new Double(text.trim()).doubleValue();
	} catch(Exception ex){
	}
	return fieldAmount;
}

public java.util.Date addMonths(java.util.Date aDate, int number){  
    java.util.Calendar aCalendar = java.util.Calendar.getInstance();  
    aCalendar.setTime(aDate);  
    aCalendar.add(java.util.Calendar.MONTH, number);  
    return aCalendar.getTime();  
}

%>
